package cn.wolfcode.car.controller;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import cn.wolfcode.car.domain.Business;
import cn.wolfcode.car.qo.BusinessQueryObject;
import cn.wolfcode.car.service.IBusinessService;
import com.github.pagehelper.PageInfo;
import org.apache.commons.math3.analysis.differentiation.SparseGradient;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import result.JsonResult;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/business")
public class BusinessController {
    @Autowired
    private IBusinessService businessService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") BusinessQueryObject qo) {
        PageInfo<Business> list = businessService.list(qo);
        System.out.println(list);
        model.addAttribute("result", list);
        List<Business> businesss = businessService.listAll();
        model.addAttribute("businesss", businesss);
        return "business/list";
    }

    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Business business, MultipartFile file, HttpServletRequest request) throws IOException {
        System.out.println("----------------" + business);
        if (file != null && !file.isEmpty()) {
            String path = request.getServletContext().getRealPath("/img");
            int rand = RandomUtil.randomInt(10000, 99999);
            String date = DateUtil.format(new Date(), "yyyyMMdd");
            String name = file.getOriginalFilename();
            String[] arr = name.split("\\.");
            String ext = arr[arr.length - 1];
            StringBuilder sb = new StringBuilder();
            sb.append(path);
            sb.append("/");
            sb.append(date);
            sb.append(rand);
            sb.append(".");
            sb.append(ext);
            //文件写入
            FileOutputStream fos = new FileOutputStream(sb.toString());
            InputStream is = file.getInputStream();
            byte[] buf = new byte[1024];
            int len = 0;
            while ((len = is.read(buf)) != -1) {
                fos.write(buf, 0, len);
            }
            fos.close();
            is.close();
            StringBuilder sb1 = new StringBuilder();
            sb1.append("/img/");
            sb1.append(date);
            sb1.append(rand);
            sb1.append(".");
            sb1.append(ext);
            business.setLicenseImg(sb1.toString());
        }
        System.out.println(business);
        if (business.getId() == null) {
            businessService.save(business);
        } else {
            businessService.update(business);
        }
        return "redirect:/business/list";
    }

    @GetMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id) {
        businessService.delete(id);
        return JsonResult.success(null);
    }

    @RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) {
            Business business = businessService.get(id);
            model.addAttribute("business", business);
        }
        return "business/input";
    }
}
